<?php

namespace App\Http\Middleware;

use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use App\Common\Service\PassportService;
use Closure;

class ApiLoginCheck {


    public function handle($requst, Closure $next) {
      $uid = $requst->uid;
      $token = $requst->token;
//      $uid = 42;
//      $token = '3bc37c0282d0e438051d16cb0ec7c36e';
//      $res = $next($requst);
//      return $res;

      $passportObj = new PassportService();
      if(empty($uid) || empty($token) ) {
          throw new AixinException(AixinExceptionCode::MISSING_PARAM);
      }

      $isLoginToken = $passportObj->isLoginToken($uid, $token);
      if(!$isLoginToken) {
          throw new AixinException(AixinExceptionCode::LOGINCHECK);
      }

      return $next($requst);

  }
}
